###Script to Code variables---------------------------
###Clear R------------------------
rm(list=ls())

###Function to recode variables to range from lowest (0) to highest (1) observation
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

#function to install packages if they don't exist
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("plm", "ggplot2", "psych", "lme4", "optimx", "stargazer", "interplot")
ipak(packages)

### Coding --- Study 1: load data, select variables and recode---------------
load("Study1_data.RData")
data_food <- data[,which(colnames(data)%in%c("DV_irradiation", "InParty", "OutParty","partyidentity","CRTall","nfc", "cogresources", "female", "age", "non_white", "Republican_dummy", "education"))]
data_food$id <- sample(747, size = nrow(data_food))
data_food$study <- 1
data_food$CRT_honest<-1
colnames(data_food)[colnames(data_food)=="DV_irradiation"] <- "PolicySupport"

#recode age into categories
data_food$age[data_food$age < 19]=1
data_food$age[data_food$age> 18 & data_food$age < 25]=2
data_food$age[data_food$age> 24 & data_food$age < 35]=3
data_food$age[data_food$age> 34 & data_food$age < 45]=4
data_food$age[data_food$age> 44 & data_food$age < 55]=5
data_food$age[data_food$age> 54 & data_food$age < 65]=6
data_food$age[data_food$age> 64 & data_food$age < 75]=7
data_food$age[data_food$age> 74]=8

### Coding --- Study 2: load data, select variables and recode---------------
load("Study2_data.RData")
data_farm <- data[,which(colnames(data)%in%c("DV1", "InParty","OutParty", "pidstrength", "CRTall","nfc", "cogresources", "female", "age", "non_white", "Republican_dummy", "education"))]
data_farm$id <- sample(1302, size = nrow(data_farm))
data_farm$id <- data_farm$id + 747
data_farm$study <- 2
data_farm$CRT_honest<-1
colnames(data_farm)[colnames(data_farm)=="DV1"] <- "PolicySupport"
colnames(data_farm)[colnames(data_farm)=="pidstrength"] <- "partyidentity"

### Coding --- Study 3 load data, select variables and recode---------------

load("Study3_data.R")
data$id <- sample(1911, size = nrow(data))
data$id <- data$id + (1302+747)
data$age[data$age < 19]=1
data$age[data$age> 18 & data$age < 25]=2
data$age[data$age> 24 & data$age < 35]=3
data$age[data$age> 34 & data$age < 45]=4
data$age[data$age> 44 & data$age < 55]=5
data$age[data$age> 54 & data$age < 65]=6
data$age[data$age> 64 & data$age < 75]=7
data$age[data$age> 74]=8

#Select Food Irradiation Experiment data from Study 3 data
data_replication_food <- data[,which(colnames(data)%in%c("DV_irradiation", "InParty_food","OutParty_food", "PSIDstrength", "CRTall","NFC", "cogresources", "female", "age", "non_white", "Republican_dummy", "edu", "id", "CRT_honest"))]
data_replication_food$study <- 3
colnames(data_replication_food)[colnames(data_replication_food)=="DV_irradiation"] <- "PolicySupport"
colnames(data_replication_food)[colnames(data_replication_food)=="PSIDstrength"] <- "partyidentity"
colnames(data_replication_food)[colnames(data_replication_food)=="NFC"] <- "nfc"
colnames(data_replication_food)[colnames(data_replication_food)=="edu"] <- "education"
colnames(data_replication_food)[colnames(data_replication_food)=="InParty_food"] <- "InParty"
colnames(data_replication_food)[colnames(data_replication_food)=="OutParty_food"] <- "OutParty"

#Select Farm Policy Experiment data  from Study 3 data
data_replication_farm <- data[,which(colnames(data)%in%c("Farm_dv_support_rec", "InParty_farm","OutParty_farm", "PSIDstrength", "CRTall","NFC", "cogresources", "female", "age", "non_white", "Republican_dummy", "edu", "id", "CRT_honest"))]
data_replication_farm$study <- 4
colnames(data_replication_farm)[colnames(data_replication_farm)=="Farm_dv_support_rec"] <- "PolicySupport"
colnames(data_replication_farm)[colnames(data_replication_farm)=="PSIDstrength"] <- "partyidentity"
colnames(data_replication_farm)[colnames(data_replication_farm)=="NFC"] <- "nfc"
colnames(data_replication_farm)[colnames(data_replication_farm)=="edu"] <- "education"
colnames(data_replication_farm)[colnames(data_replication_farm)=="InParty_farm"] <- "InParty"
colnames(data_replication_farm)[colnames(data_replication_farm)=="OutParty_farm"] <- "OutParty"


### Coding --- Study 4 load data, select variables and recode---------------
load("Study4_data.Rdata")
data$id <- sample(2509, size = nrow(data))
data$id <- data$id + (1302+747+1911+1911)

data_mturk <- data[,which(colnames(data)%in%c("DV_irradiation", "InParty","OutParty", "PSIDstrength", "CRTall", "female", "Age", "non_white", "Republican_dummy", "Education", "id"))]
data_mturk$study <- 5
data_mturk$CRT_honest<-1
data_mturk$nfc<-NA
data_mturk$cogresources<-NA
colnames(data_mturk)[colnames(data_mturk)=="DV_irradiation"] <- "PolicySupport"
colnames(data_mturk)[colnames(data_mturk)=="Education"] <- "education"
colnames(data_mturk)[colnames(data_mturk)=="PSIDstrength"] <- "partyidentity"
colnames(data_mturk)[colnames(data_mturk)=="Age"] <- "age"
colnames(data_mturk)[colnames(data_mturk)=="Repbulican_dummy"] <- "Republican_dummy"

### Pool data --- Create data sets for pooled analyses--------------
data_meta_control_inout <- rbind(data_farm,data_food,data_replication_food, data_replication_farm, data_mturk) #CRT & Cog resources data
data_meta_control_inout<-data_meta_control_inout[!(data_meta_control_inout$CRT_honest==0),] #exclude people who indicated in Study 3 that they were not going to answer CRT honstely

data_meta_control_inout_nfc <- rbind(data_farm,data_food,data_replication_food, data_replication_farm) #NfC data

### Table 4 --- Analyses belonging to Table 4 from the main text-------------------

data_meta_control_inout$study<-as.factor(data_meta_control_inout$study)
data_meta_control_inout$education<-as.factor(data_meta_control_inout$education)

summary(pooled_crt_2<-lmer(PolicySupport~InParty*CRTall+ OutParty*CRTall+ InParty*partyidentity+ OutParty*partyidentity + partyidentity*CRTall+ study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_nfc_2<-lmer(PolicySupport~InParty*nfc+ OutParty*nfc+ InParty*partyidentity+ OutParty*partyidentity + partyidentity*nfc + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_cog_2<-lmer(PolicySupport~InParty*cogresources+ OutParty*cogresources+ InParty*partyidentity+ OutParty*partyidentity + partyidentity*cogresources + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))

summary(pooled_crt<-lmer(PolicySupport~InParty*partyidentity*CRTall + OutParty*partyidentity*CRTall + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_nfc<-lmer(PolicySupport~InParty*partyidentity*nfc + OutParty*partyidentity*nfc + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_cog<-lmer(PolicySupport~InParty*partyidentity*cogresources + OutParty*partyidentity*cogresources + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))


data_meta_control_inout_tableNFC<-data_meta_control_inout
data_meta_control_inout_tableNFC$CRTall<-data_meta_control_inout_tableNFC$nfc
data_meta_control_inout_tableCOG<-data_meta_control_inout
data_meta_control_inout_tableCOG$CRTall<-data_meta_control_inout_tableCOG$cogresources

#twoway models
summary(pooled_crt_2<-lmer(PolicySupport~InParty+ OutParty + partyidentity + CRTall + InParty*CRTall + OutParty*CRTall + InParty*partyidentity + OutParty*partyidentity + CRTall*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_nfc_2<-lmer(PolicySupport~InParty+ OutParty + partyidentity + CRTall + InParty*CRTall + OutParty*CRTall + InParty*partyidentity + OutParty*partyidentity + CRTall*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_tableNFC , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_cog_2<-lmer(PolicySupport~InParty+ OutParty + partyidentity + CRTall + InParty*CRTall + OutParty*CRTall + InParty*partyidentity + OutParty*partyidentity + CRTall*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_tableCOG , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))

#three-way models
summary(pooled_crt<-lmer(PolicySupport~InParty+ OutParty + partyidentity + CRTall + InParty*CRTall + OutParty*CRTall + InParty*partyidentity + OutParty*partyidentity + CRTall*partyidentity + InParty*CRTall*partyidentity + OutParty*CRTall*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_nfc<-lmer(PolicySupport~InParty+ OutParty + partyidentity + CRTall + InParty*CRTall + OutParty*CRTall + InParty*partyidentity + OutParty*partyidentity + CRTall*partyidentity + InParty*CRTall*partyidentity + OutParty*CRTall*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_tableNFC , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_cog<-lmer(PolicySupport~InParty+ OutParty + partyidentity + CRTall + InParty*CRTall + OutParty*CRTall + InParty*partyidentity + OutParty*partyidentity + CRTall*partyidentity + InParty*CRTall*partyidentity + OutParty*CRTall*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_tableCOG , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))

stargazer(pooled_crt_2, pooled_crt, pooled_nfc_2, pooled_nfc, pooled_cog_2, pooled_cog, title="Individual Data Meta-Analysis", align=TRUE,  order=c(1,2,3,4,17, 18, 15, 16, 19, 20,21, 5,6,7,8,9,10,11,12, 13,14),covariate.labels=c("In-party cue", "Out-party cue", "Partisan Identity Strength","Cognitive resource", "In-party * PSID", "Out-party * PSID", "In-party * Cognitive", "Out-party * Cognitive", "PSID * Cognitive", "In-party * PSID * Cognitive", "Out-party * PSID * Cognitive", "Sample: Study 2", "Sample: Study 3 Food", "Sample: Study 3 Farm", "Sample: MTurk", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican", "Constant"),  omit.stat=c("LL","ser","f", "adj.rsq"),  notes.append = FALSE, star.cutoffs=c(0.1, 0.05), star.char = c("+", "*"), notes = "+p<.1; *p<0.05", column.sep.width = "1pt",  no.space=TRUE,font.size="tiny" , out = "Meta.tex",dep.var.caption = "Policy support",column.labels = c("CRT", "NFC", "Cog resources"), column.separate = c(2, 2, 2), dep.var.labels.include = F,label="tab:meta", digits=2)

### Figure 3 --- Analyses belonging to Figure 3 in the main text-------------------
m0_nfc <- lmer(PolicySupport~InParty*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity+ OutParty*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity  + study + age + female + education + non_white + Republican_dummy  + (1|study)+ (1|id),data=data_meta_control_inout_nfc , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='partyidentity')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='partyidentity')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'
## NfC at mean
m1_nfc <- lmer(PolicySupport~InParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity+OutParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_nfc , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='partyidentity')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='partyidentity')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'

## NfC at +1
m2_nfc <- lmer(PolicySupport~InParty*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity+ OutParty*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_nfc , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
summary(m2_nfc)
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='partyidentity')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='partyidentity')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'

forplot_nfc <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_nfc$battery      <- c("NfC")
forplot_nfc$CRT <- factor(forplot_nfc$CRT,levels = c("-1 SD","Mean","+1 SD"))

##cog resources
data_meta_control_inout_cog <- subset(data_meta_control_inout,is.na(cogresources)==F) #excclude missing data


m0_cog <- lmer(PolicySupport~InParty*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity+ OutParty*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity  + study + age + female + education + non_white + Republican_dummy  + (1|study)+ (1|id),data=data_meta_control_inout_cog , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_0_in_cog<- interplot(m0_cog,var1 = "InParty",var2='partyidentity')
m_0_in_cog$data$CRT='-1 SD'
m_0_in_cog$data$Cue='In'
m_0_out_cog<- interplot(m0_cog,var1 = "OutParty",var2='partyidentity')
m_0_out_cog$data$CRT='-1 SD'
m_0_out_cog$data$Cue='Out'

## Cog at mean
m1_cog <- lmer(PolicySupport~InParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity+OutParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_cog , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_1_in_cog <- interplot(m1_cog,var1 = "InParty",var2='partyidentity')
m_1_in_cog$data$CRT='Mean'
m_1_in_cog$data$Cue='In'
m_1_out_cog <- interplot(m1_cog,var1 = "OutParty",var2='partyidentity')
m_1_out_cog$data$CRT='Mean'
m_1_out_cog$data$Cue='Out'

## Cog at +1
m2_cog<- lmer(PolicySupport~InParty*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity+ OutParty*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_cog , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
summary(m2_cog)
m_2_in_cog <- interplot(m2_cog,var1 = "InParty",var2='partyidentity')
m_2_in_cog$data$CRT='+1 SD'
m_2_in_cog$data$Cue='In'
m_2_out_cog <- interplot(m2_cog,var1 = "OutParty",var2='partyidentity')
m_2_out_cog$data$CRT='+1 SD'
m_2_out_cog$data$Cue='Out'

forplot_cog <- rbind(m_0_in_cog$data, m_0_out_cog$data,m_1_in_cog$data,m_1_out_cog$data,m_2_in_cog$data,m_2_out_cog$data)
forplot_cog$battery      <- c("Cognitive\n resources")
forplot_cog$CRT <- factor(forplot_cog$CRT,levels = c("-1 SD","Mean","+1 SD"))

## CRT -1SD
data_meta_control_inout_crt <- subset(data_meta_control_inout,is.na(CRTall)==F) #excclude missing data

m0_crt <- lmer(PolicySupport~InParty*scale(CRTall,center=mean(CRTall)-sd(CRTall),scale=F)*partyidentity+ OutParty*scale(CRTall,center=mean(CRTall)-sd(CRTall),scale=F)*partyidentity  + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_crt , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_0_in_crt <- interplot(m0_crt,var1 = "InParty",var2='partyidentity')
m_0_in_crt$data$CRT='-1 SD'
m_0_in_crt$data$Cue='In'
m_0_out_crt <- interplot(m0_crt,var1 = "OutParty",var2='partyidentity')
m_0_out_crt$data$CRT='-1 SD'
m_0_out_crt$data$Cue='Out'

## CRT at mean
m1_crt <- lmer(PolicySupport~InParty*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity+OutParty*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_crt , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_1_in_crt <- interplot(m1_crt,var1 = "InParty",var2='partyidentity')
m_1_in_crt$data$CRT='Mean'
m_1_in_crt$data$Cue='In'
m_1_out_crt <- interplot(m1_crt,var1 = "OutParty",var2='partyidentity')
m_1_out_crt$data$CRT='Mean'
m_1_out_crt$data$Cue='Out'

## CRT at +1
m2_crt <- lmer(PolicySupport~InParty*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity+ OutParty*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout_crt , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
summary(m2_crt)
m_2_in_crt <- interplot(m2_crt,var1 = "InParty",var2='partyidentity')
m_2_in_crt$data$CRT='+1 SD'
m_2_in_crt$data$Cue='In'
m_2_out_crt <- interplot(m2_crt,var1 = "OutParty",var2='partyidentity')
m_2_out_crt$data$CRT='+1 SD'
m_2_out_crt$data$Cue='Out'

forplot_crt <- rbind(m_0_in_crt$data, m_0_out_crt$data,m_1_in_crt$data,m_1_out_crt$data,m_2_in_crt$data,m_2_out_crt$data)
forplot_crt$battery      <- c("CRT")
forplot_crt$CRT <- factor(forplot_crt$CRT,levels = c("-1 SD","Mean","+1 SD"))

#combine plots
forplot_comb <- rbind(forplot_crt, forplot_nfc, forplot_cog)
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NfC","Cognitive\n resources"))

ggplot(forplot_comb,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of In-party cue and Out-party cue on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black"))+scale_y_continuous(limits = c(-.2, .2))+ theme(strip.text.y = element_text(angle = 360), legend.position="bottom")
#save results
ggsave("Meta_analysis.pdf",width=8,height=6)



### Appendix E.1 Distribution of partisan social identity strength---------------

ggplot(data_meta_control_inout, aes(x=partyidentity, fill=study))+ geom_density( alpha = 0.35) + scale_fill_discrete("Study", breaks=c("1", "2", "3", "4"), labels=c("Study 1: Food", "Study 2: Farm", "Study 3: Food & Farm", "Study 4: Food"))+ theme_bw() + xlab("Partisan Social Identity Strength") + ylab("Density")
ggsave("PSID_distributions.pdf",width=8,height=6)

### Appendix E.2 Main effects of the meta-analysis---------------------

summary(main_crt_2<-lmer(PolicySupport~InParty + OutParty + partyidentity+ CRTall + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(main_nfc_2<-lmer(PolicySupport~InParty + OutParty + partyidentity+ nfc + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id), data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(main_cog_2<-lmer(PolicySupport~InParty + OutParty + partyidentity+ cogresources + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=data_meta_control_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))

stargazer(main_crt_2, main_nfc_2, main_cog_2, title="Individual Data Meta-Analysis: Main effects", align=TRUE,  order=c(1,2,3,4, 5,6,7,8,9,10,11,12, 13,14),covariate.labels=c("In-party cue", "Out-party cue", "Partisan Identity Strength (PSID)","CRT", "NFC", "Cognitive resource", "Sample: Study 2", "Sample: Study 3 Food Experiment", "Sample: Study 3 Farm Experiment", "Sample: MTurk", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican", "Constant"),  omit.stat=c("LL","ser","f", "adj.rsq"), notes.append = FALSE, star.cutoffs=c(0.1, 0.05), star.char = c("+", "*"), notes = "+p<.1; *p<0.05", column.sep.width = "1pt", no.space=TRUE,font.size="tiny" , out = "Meta_main.tex",dep.var.caption = "Policy support",column.labels = c("CRT", "NFC", "Cog resources"), column.separate = c(1, 1, 1), dep.var.labels.include = F,label="tab:meta", digits=2)



### Appendix E.3 Meta-analysis In-party vs. Out-party cue: load data Study 1: Food Irradiation ----------
load("Study1_data.RData")
data_food <- data[,which(colnames(data)%in%c("DV_irradiation", "In_vs_OutParty","partyidentity","CRTall","nfc", "cogresources", "female", "age", "non_white", "Republican_dummy", "education", "PID"))]
#data_food$id <- sample(747, size = nrow(data_food))
data_food$study <- 1
data_food$CRT_honest<-1
colnames(data_food)[colnames(data_food)=="DV_irradiation"] <- "PolicySupport"
colnames(data_food)[colnames(data_food)=="PID"] <- "id"
#recode age into categories
data_food$age[data_food$age < 19]=1
data_food$age[data_food$age> 18 & data_food$age < 25]=2
data_food$age[data_food$age> 24 & data_food$age < 35]=3
data_food$age[data_food$age> 34 & data_food$age < 45]=4
data_food$age[data_food$age> 44 & data_food$age < 55]=5
data_food$age[data_food$age> 54 & data_food$age < 65]=6
data_food$age[data_food$age> 64 & data_food$age < 75]=7
data_food$age[data_food$age> 74]=8
data_food$female_mis<-0
data_food$age_mis<-0
data_food$non_white_mis<-0
data_food$Republican_dummy_mis<-0
data_food$education_mis<-0

### Appendix E.3 Meta-analysis In-party vs. Out-party cue: load data Study 2: Farm Policy ----------
load("Study2_data.RData")
data$In_vs_OutParty<-ifelse(data$InParty==1,1,
                            ifelse(data$OutParty==1,0, NA))
data_farm <- data[,which(colnames(data)%in%c("DV1", "In_vs_OutParty", "pidstrength", "CRTall","nfc", "cogresources", "female", "age", "non_white", "Republican_dummy", "education"))]
data_farm$id <- sample(1302, size = nrow(data_farm))
data_farm$id <- data_farm$id + 747
data_farm$study <- 2
data_farm$CRT_honest<-1
colnames(data_farm)[colnames(data_farm)=="DV1"] <- "PolicySupport"
colnames(data_farm)[colnames(data_farm)=="pidstrength"] <- "partyidentity"
data_farm$female_mis<-0
data_farm$age_mis<-0
data_farm$non_white_mis<-0
data_farm$Republican_dummy_mis<-0
data_farm$education_mis<-0
### Appendix E.3 Meta-analysis In-party vs. Out-party cue: load data Study 3: Food and Farm Policy Study ----------

load("Study3_data.R")
data$id <- sample(1911, size = nrow(data))
data$id <- data$id + (1302+747)
data$age[data$age < 19]=1
data$age[data$age> 18 & data$age < 25]=2
data$age[data$age> 24 & data$age < 35]=3
data$age[data$age> 34 & data$age < 45]=4
data$age[data$age> 44 & data$age < 55]=5
data$age[data$age> 54 & data$age < 65]=6
data$age[data$age> 64 & data$age < 75]=7
data$age[data$age> 74]=8
data$In_vs_OutParty_food<-ifelse(data$InParty_food==1,1,
                            ifelse(data$OutParty_food==1,0, NA))
#Select Food Irradiation Experiment data from Study 3 data
data_replication_food <- data[,which(colnames(data)%in%c("DV_irradiation", "In_vs_OutParty_food", "PSIDstrength", "CRTall","NFC", "cogresources", "female", "age", "non_white", "Republican_dummy", "edu", "id", "CRT_honest"))]
data_replication_food$study <- 3
colnames(data_replication_food)[colnames(data_replication_food)=="In_vs_OutParty_food"] <- "In_vs_OutParty"
colnames(data_replication_food)[colnames(data_replication_food)=="DV_irradiation"] <- "PolicySupport"
colnames(data_replication_food)[colnames(data_replication_food)=="PSIDstrength"] <- "partyidentity"
colnames(data_replication_food)[colnames(data_replication_food)=="NFC"] <- "nfc"
colnames(data_replication_food)[colnames(data_replication_food)=="edu"] <- "education"
colnames(data_replication_food)[colnames(data_replication_food)=="InParty_food"] <- "InParty"
colnames(data_replication_food)[colnames(data_replication_food)=="OutParty_food"] <- "OutParty"
data_replication_food$female_mis<-0
data_replication_food$age_mis<-0
data_replication_food$non_white_mis<-0
data_replication_food$Republican_dummy_mis<-0
data_replication_food$education_mis<-0

#Select Farm Policy Experiment data  from Study 3 data
data$In_vs_OutParty_farm<-ifelse(data$InParty_farm==1,1,
                                 ifelse(data$OutParty_farm==1,0, NA))
data_replication_farm <- data[,which(colnames(data)%in%c("Farm_dv_support_rec", "In_vs_OutParty_farm", "PSIDstrength", "CRTall","NFC", "cogresources", "female", "age", "non_white", "Republican_dummy", "edu", "id", "CRT_honest"))]
data_replication_farm$study <- 4
colnames(data_replication_farm)[colnames(data_replication_farm)=="In_vs_OutParty_farm"] <- "In_vs_OutParty"
colnames(data_replication_farm)[colnames(data_replication_farm)=="Farm_dv_support_rec"] <- "PolicySupport"
colnames(data_replication_farm)[colnames(data_replication_farm)=="PSIDstrength"] <- "partyidentity"
colnames(data_replication_farm)[colnames(data_replication_farm)=="NFC"] <- "nfc"
colnames(data_replication_farm)[colnames(data_replication_farm)=="edu"] <- "education"
colnames(data_replication_farm)[colnames(data_replication_farm)=="InParty_farm"] <- "InParty"
colnames(data_replication_farm)[colnames(data_replication_farm)=="OutParty_farm"] <- "OutParty"
data_replication_farm$female_mis<-0
data_replication_farm$age_mis<-0
data_replication_farm$non_white_mis<-0
data_replication_farm$Republican_dummy_mis<-0
data_replication_farm$education_mis<-0

### Appendix E.3 Meta-analysis In-party vs. Out-party cue: load data Study 4: Food Irradiation---------------
load("Study4_data.Rdata")
data$id <- sample(2509, size = nrow(data))
data$id <- data$id + (1302+747+1911+1911)
data$In_vs_OutParty<-ifelse(data$InParty==1,1,
                                 ifelse(data$OutParty==1,0, NA))
data_mturk <- data[,which(colnames(data)%in%c("DV_irradiation", "In_vs_OutParty", "PSIDstrength", "CRTall", "female", "Age", "non_white", "Republican_dummy", "Education", "id"))]
data_mturk$study <- 5
data_mturk$CRT_honest<-1
data_mturk$nfc<-NA
data_mturk$cogresources<-NA
colnames(data_mturk)[colnames(data_mturk)=="DV_irradiation"] <- "PolicySupport"
colnames(data_mturk)[colnames(data_mturk)=="Education"] <- "education"
colnames(data_mturk)[colnames(data_mturk)=="PSIDstrength"] <- "partyidentity"
colnames(data_mturk)[colnames(data_mturk)=="Age"] <- "age"
colnames(data_mturk)[colnames(data_mturk)=="Repbulican_dummy"] <- "Republican_dummy"
data_mturk$female_mis<-0
data_mturk$age_mis<-0
data_mturk$non_white_mis<-0
data_mturk$Republican_dummy_mis<-0
data_mturk$education_mis<-0

### Appendix E.3 Meta-analysis In-party vs. Out-party cue: load data Study 5: Trade Policy Experiment----------
load("Study5_TTIP_data.RData")
#data$id <- sample(888, size = nrow(data))
#data$id <- data$id + (1302+747+2509+1911+1911)
data_ttip <- data[,which(colnames(data)%in%c("supportttip", "ownpartysupport","partyidentity","CRTall","nfc", "cogresources", "female", "age", "non_white", "Republican_dummy", "education", "PID"))]
data_ttip$study <- 6
data_ttip$CRT_honest<-1
colnames(data_ttip)[colnames(data_ttip)=="PID"] <- "id"
colnames(data_ttip)[colnames(data_ttip)=="supportttip"] <- "PolicySupport"
colnames(data_ttip)[colnames(data_ttip)=="ownpartysupport"] <- "In_vs_OutParty"
data_ttip$female_mis<-0
data_ttip$age_mis<-0
data_ttip$non_white_mis<-0
data_ttip$Republican_dummy_mis<-0
data_ttip$education_mis<-0

### Appendix E.3 Meta-analysis In-party vs. Out-party cue: load data Study 6: Danish Experiment----------
load("Study6_data.RData")
data$id <- sample(1828, size = nrow(data))
data$id <- data$id + (1302+747+2509+1911+1911+888)
data_dk <- data[,which(colnames(data)%in%c("supportttip", "inparty","CRTall", "identitystrength", "id", "complex"))]
colnames(data_dk)[colnames(data_dk)=="supportttip"] <- "PolicySupport"
colnames(data_dk)[colnames(data_dk)=="inparty"] <- "In_vs_OutParty"
colnames(data_dk)[colnames(data_dk)=="identitystrength"] <- "partyidentity"
data_dk$nfc<-NA
data_dk$cogresources<-NA
data_dk$female<-2
data_dk$age<-1
data_dk$non_white<-2
data_dk$Republican_dummy<-2
data_dk$education<-4
data_dk$CRT_honest<-1

data_dk$female_mis<-1
data_dk$age_mis<-1
data_dk$non_white_mis<-1
data_dk$Republican_dummy_mis<-1
data_dk$education_mis<-1

#subset on issue
data_dk_pill<-subset(data_dk, complex=="Pill")
data_dk_pill$study <- 7
data_dk_pill<-data_dk_pill[,-1]
data_dk_ttip<-subset(data_dk, complex=="TIPI")
data_dk_ttip$study <- 8
data_dk_ttip<-data_dk_ttip[,-1]


### Appendix E.3 Meta-analysis In-party vs. Out-party Cue; merge data--------------
meta_inout <- rbind(data_farm,data_food,data_replication_food,data_replication_farm, data_mturk, data_ttip, data_dk_pill, data_dk_ttip)
meta_inout$study<-as.factor(meta_inout$study)
meta_inout$education<-as.factor(meta_inout$education)
meta_inout<-meta_inout[!(meta_inout$CRT_honest==0),] #exclude people who indicated in Study 3 that they were not going to answer CRT honstely

meta_inout_nfc <- rbind(data_farm,data_food,data_replication_food,data_replication_farm, data_ttip)
meta_inout_nfc$study<-as.factor(meta_inout_nfc$study)
meta_inout_nfc$education<-as.factor(meta_inout_nfc$education)

### Exclude MTurk---
meta_inout_turk <- rbind(data_farm,data_food,data_replication_food,data_replication_farm, data_ttip, data_dk_pill, data_dk_ttip)


### Appendix E.3 Meta-analysis In-party vs. Out-party: Table-------------

#twoway
summary(pooled_crt_2<-lmer(PolicySupport~In_vs_OutParty*CRTall+ In_vs_OutParty*partyidentity + partyidentity*CRTall+ study + age + female + education + non_white + Republican_dummy + female_mis + age_mis + non_white_mis + Republican_dummy_mis + education_mis + (1|study) + (1|id),data=meta_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_nfc_2<-lmer(PolicySupport~In_vs_OutParty*nfc+ In_vs_OutParty*partyidentity + partyidentity*nfc+ study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_cog_2<-lmer(PolicySupport~In_vs_OutParty*cogresources+ In_vs_OutParty*partyidentity + partyidentity*cogresources+ study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))

summary(pooled_crt)
#threeway
summary(pooled_crt<-lmer(PolicySupport~In_vs_OutParty*partyidentity*CRTall + study + age + female + education + non_white + Republican_dummy + female_mis + age_mis + non_white_mis + Republican_dummy_mis + education_mis +(1|study) + (1|id),data=meta_inout_turk , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_nfc<-lmer(PolicySupport~In_vs_OutParty*partyidentity*nfc + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(pooled_cog<-lmer(PolicySupport~In_vs_OutParty*partyidentity*cogresources + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))

meta_inoutNFC<-meta_inout
meta_inoutNFC$CRTall<-meta_inout$nfc
meta_inoutCOG<-meta_inout
meta_inoutCOG$CRTall<-meta_inout$cogresources

results<-list()
summary(results[[4]]<-lmer(PolicySupport~In_vs_OutParty*CRTall+ In_vs_OutParty*partyidentity + partyidentity*CRTall+ study + age + female + education + non_white + Republican_dummy +(1|study) + (1|id),data=meta_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(results[[5]]<-lmer(PolicySupport~In_vs_OutParty*CRTall+ In_vs_OutParty*partyidentity + partyidentity*CRTall+ study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inoutNFC , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(results[[6]]<-lmer(PolicySupport~In_vs_OutParty*CRTall+ In_vs_OutParty*partyidentity + partyidentity*CRTall+ study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inoutCOG , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))

summary(results[[7]]<-lmer(PolicySupport~In_vs_OutParty*CRTall+ In_vs_OutParty*partyidentity + partyidentity*CRTall+ In_vs_OutParty*CRTall*partyidentity+ study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(results[[8]]<-lmer(PolicySupport~In_vs_OutParty*CRTall+ In_vs_OutParty*partyidentity + partyidentity*CRTall+ In_vs_OutParty*CRTall*partyidentity+ study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id), data=meta_inoutNFC , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))
summary(results[[9]]<-lmer(PolicySupport~In_vs_OutParty*CRTall+ In_vs_OutParty*partyidentity + partyidentity*CRTall+ In_vs_OutParty*CRTall*partyidentity+ study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inoutCOG , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B'))))

stargazer(results[[4]], results[[7]], results[[5]], results[[8]],results[[6]], results[[9]], title="Individual Data Meta-Analysis: In-party versus Out-party Cue", align=TRUE,order=c(1,3,2, 18, 17, 19, 20 ,4,5,6,7,8,9,10,11,12, 13,14, 15, 16)  ,covariate.labels=c("In-party cue", "Partisan Identity Strength (PSID)","Cognitive resource", "In-party * PSID", "In-party * Cognitive", "PSID * Cognitive", "In-party * PSID * Cognitive", "Sample: Study 2", "Sample: Study 3 Food Experiment", "Sample: Study 3 Farm Experiment", "Sample: MTurk", "Sample: TTIP study", "Sample: Danish TTIP experiment", "Sample: Danish Euthanasia experiment", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican", "Constant"),  omit.stat=c("LL","ser","f", "adj.rsq"), notes.append = FALSE, star.cutoffs=c(0.1, 0.05), star.char = c("+", "*"),  notes = "+p<.1; *p<0.05", column.sep.width = "1pt", no.space=TRUE,font.size="tiny" , out = "Meta_inout.tex",dep.var.caption = "Policy support",column.labels = c("CRT", "NFC", "Cog resources"), column.separate = c(2, 2, 2), dep.var.labels.include = F,label="tab:meta_inout", digits=2)


### Appendix E.3 Meta-analysis In-party vs. Out-party: Figure-------------
meta_inout_crt<-meta_inout
meta_inout_crt <- subset(meta_inout_crt,is.na(CRTall)==F) #excclude missing data

#CRT resources
m0_crt <- lmer(PolicySupport~In_vs_OutParty*scale(CRTall,center=mean(CRTall)-sd(CRTall),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy  + female_mis + age_mis + non_white_mis + Republican_dummy_mis + education_mis + (1|study)+ (1|id),data=meta_inout_crt , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_0_in_crt <- interplot(m0_crt,var1 = "In_vs_OutParty",var2='partyidentity')
m_0_in_crt$data$CRT='-1 SD'
m_0_in_crt$data$Cue='In'

## CRT at mean
m1_crt <- lmer(PolicySupport~In_vs_OutParty*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*partyidentity + study + age + female + education + non_white  + Republican_dummy + female_mis + age_mis + non_white_mis + Republican_dummy_mis + education_mis + (1|study) + (1|id),data=meta_inout_crt , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_1_in_crt <- interplot(m1_crt,var1 = "In_vs_OutParty",var2='partyidentity')
m_1_in_crt$data$CRT='Mean'
m_1_in_crt$data$Cue='In'

## CRT at +1
m2_crt <- lmer(PolicySupport~In_vs_OutParty*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + female_mis + age_mis + non_white_mis + Republican_dummy_mis + education_mis + (1|study) + (1|id),data=meta_inout_crt , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
summary(m2_crt)
m_2_in_crt <- interplot(m2_crt,var1 = "In_vs_OutParty",var2='partyidentity')
m_2_in_crt$data$CRT='+1 SD'
m_2_in_crt$data$Cue='In'

forplot_crt <- rbind(m_0_in_crt$data,m_1_in_crt$data,m_2_in_crt$data)
forplot_crt$battery      <- c("CRT")
forplot_crt$CRT <- factor(forplot_crt$CRT,levels = c("-1 SD","Mean","+1 SD"))


m0_nfc <- lmer(PolicySupport~In_vs_OutParty*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy  + (1|study)+ (1|id),data=meta_inout_nfc , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_0_in_nfc <- interplot(m0_nfc,var1 = "In_vs_OutParty",var2='partyidentity')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'

## NfC at mean
m1_nfc <- lmer(PolicySupport~In_vs_OutParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout_nfc , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_1_in_nfc <- interplot(m1_nfc,var1 = "In_vs_OutParty",var2='partyidentity')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'

## NfC at +1
m2_nfc <- lmer(PolicySupport~In_vs_OutParty*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout_nfc , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
summary(m2_nfc)
m_2_in_nfc <- interplot(m2_nfc,var1 = "In_vs_OutParty",var2='partyidentity')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'

forplot_nfc <- rbind(m_0_in_nfc$data,m_1_in_nfc$data,m_2_in_nfc$data)
forplot_nfc$battery      <- c("NFC")
forplot_nfc$CRT <- factor(forplot_nfc$CRT,levels = c("-1 SD","Mean","+1 SD"))

#Cog resources
meta_inout_cog<-meta_inout
meta_inout_cog <- subset(meta_inout_cog,is.na(cogresources)==F) #excclude missing data

m0_cog <- lmer(PolicySupport~In_vs_OutParty*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy  + (1|study)+ (1|id),data=meta_inout_cog , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_0_in_cog <- interplot(m0_cog,var1 = "In_vs_OutParty",var2='partyidentity')
m_0_in_cog$data$CRT='-1 SD'
m_0_in_cog$data$Cue='In'

## Cog resources at mean
m1_cog <- lmer(PolicySupport~In_vs_OutParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout_cog , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
m_1_in_cog <- interplot(m1_cog,var1 = "In_vs_OutParty",var2='partyidentity')
m_1_in_cog$data$CRT='Mean'
m_1_in_cog$data$Cue='In'

## Cog resources at +1
m2_cog <- lmer(PolicySupport~In_vs_OutParty*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity + study + age + female + education + non_white + Republican_dummy + (1|study) + (1|id),data=meta_inout_cog , REML = FALSE, control = lmerControl(optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
summary(m2_cog)
m_2_in_cog <- interplot(m2_cog,var1 = "In_vs_OutParty",var2='partyidentity')
m_2_in_cog$data$CRT='+1 SD'
m_2_in_cog$data$Cue='In'

forplot_cog <- rbind(m_0_in_cog$data,m_1_in_cog$data,m_2_in_cog$data)
forplot_cog$battery      <- c("Cognitive\n resources")
forplot_cog$CRT <- factor(forplot_nfc$CRT,levels = c("-1 SD","Mean","+1 SD"))

forplot<-rbind(forplot_crt, forplot_nfc, forplot_cog)
forplot$battery <- factor(forplot$battery ,levels = c("CRT","NFC","Cognitive\n resources"))
ggplot(forplot,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of In-party cue versus the Out-party cue on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black"))+scale_y_continuous(limits = c(-.1, .3))+ theme(strip.text.y = element_text(angle = 360), legend.position="bottom")
#save results
ggsave("Meta_analysis_in_out.pdf",width=8,height=6)

